import types from './types'
import axios from 'axios'
import Rest from '@/service/rest'

export default {
  /**
   * 获取列表
   */
  getUsers ({commit}, params) {
    let url = '/user'
    return new Promise((resolve, reject) => {
      axios.get(url, {
        params
      })
      .then(res => {
        let data = res.data
        commit(types.SET_USERS, data)
        resolve(data)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 获取详情
   */
  getUser ({commit}, {username}) {
    let url = '/user/' + username
    return new Promise((resolve, reject) => {
      axios.get(url, {
      })
      .then(res => {
        commit(types.SET_USER, res.data)
        resolve(res.data)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 新增
   */
  postUser ({commit}, user) {
    let url = '/user'
    return new Promise((resolve, reject) => {
      axios.post(url, user, {})
      .then(res => {
        let item = res.data
        commit(types.ADD_USER, item)
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 编辑
   */
  putUser ({commit}, user) {
    let url = '/user/' + user.username
    return new Promise((resolve, reject) => {
      axios.put(url, user, {})
      .then(res => {
        let item = res.data
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 删除
   */
  deleteUser ({commit}, {username}) {
    let url = '/user/' + username
    return new Promise((resolve, reject) => {
      axios.delete(url)
      .then(res => {
        commit(types.DELETE_USER, username)
        resolve(username)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  enableUser ({commit}, {username, enabled}) {
    let url = '/user/' + username + '/enable'
    return new Promise((resolve, reject) => {
      axios.put(url, {enabled}, {})
      .then(res => {
        let item = res.data
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  resetPassword ({commit}, user) {
    let url = '/password'
    return new Promise((resolve, reject) => {
      axios.put(url, user, {})
      .then(res => {
        let item = res.data
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  },

  /**
   * 修改用户个人密码
   */
  changePassword ({commit}, {username, old, password}) {
    let url = '/user/' + username + '/password'
    return new Promise((resolve, reject) => {
      axios.put(url, {username, old, password}, {})
      .then(res => {
        let item = res.data
        resolve(item)
      })
      .catch(error => {
        Rest.errorHandler(error)
        reject(error)
      })
    })
  }
}
